package app.purchase.a571xz.com.myandroidframe.business.login;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import app.purchase.a571xz.com.myandroidframe.base.mvp.BasePresenter;
import app.purchase.a571xz.com.myandroidframe.httpservice.response.LoginResponse;
import app.purchase.a571xz.com.myandroidframe.httpservice.retrofit.ApiSubscriberCallback;
import app.purchase.a571xz.com.myandroidframe.repository.NetworkRepository;
import app.purchase.a571xz.com.myandroidframe.utils.RxLifecycleUtil;

public class LoginPresenter extends BasePresenter<LoginContact.ILoginModel, LoginContact.ILoginView> {

    public LoginPresenter(LoginContact.ILoginView iView) {
        super(iView);
        mModel = new LoginModel();
    }

    public void login(String username, String password) {
        mRootView.showLoading();
        NetworkRepository.getInstance().commonRequest(mModel.login(username, password), new ApiSubscriberCallback<LoginResponse>() {
            @Override
            public void onResult(LoginResponse loginResponse) {
                mRootView.showMessage(loginResponse.getUsers().getUserNick());
            }

            @Override
            public void onFailure(Throwable t) {
                mRootView.showMessage(t.getMessage());
            }

            @Override
            public void onCompleted() {
                mRootView.hideLoading();
            }
        }, RxLifecycleUtil.bindUntilEvent(mRootView, ActivityEvent.DESTROY));
    }

}
